package sv.com.seguros.web.controller.asg;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import sv.com.seguros.dto.asg.AsgContacto;
import sv.com.seguros.exception.WebException;
import sv.com.seguros.web.controller.BaseController;
import sv.com.seguros.web.util.DBContext;
import sv.com.seguros.web.util.WebUtilidades;

@Controller
@RequestMapping("/contactos")
public class AsgContactoController extends BaseController {

	private static final long serialVersionUID = 3834669882649782965L;
	
	@RequestMapping(value = "/save", method = {RequestMethod.POST})
	public void save(
			@ModelAttribute AsgContacto asgContacto, 
			HttpServletResponse response, 
			@RequestParam Long identifier, 
			@RequestParam String type){
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			asgContacto.setSgdUsuario(WebUtilidades.getSgdUsuarioFromAcegi());
			DBContext.getBaseDao().save(asgContacto);
			result.put("contactos", DBContext.getAsgContactoDao().findAllByType(identifier, type));
			result.put(SUCCESS, true);
			result.put(MESSAGE, UPDATED_DATA);
		}catch(WebException e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		}catch(Exception e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/delete", method = {RequestMethod.POST})
	public void delete(
			@RequestParam Long asgContactoId, 
			@RequestParam Long identifier, 
			@RequestParam String type, 
			HttpServletResponse response){
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			DBContext.getAsgContactoDao().delete(asgContactoId);
			result.put("contactoss", DBContext.getAsgContactoDao().findAllByType(identifier, type));
			result.put(SUCCESS, true);
			result.put(MESSAGE, UPDATED_DATA);
		}catch(WebException e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		}catch(Exception e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/findByType", method = {RequestMethod.POST})
	public void findByAsgReclamoId(HttpServletResponse response,
			@RequestParam Long identifier, 
			@RequestParam String type){
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			result.put("contactos", DBContext.getAsgContactoDao().findAllByType(identifier, type));
			result.put(SUCCESS, true);
			result.put(MESSAGE, "Datos obtenidos");
		}catch(WebException e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		}catch(Exception e){
			logger.error("Exception:", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}

	@RequestMapping(value = "/deleteContacto", method = {RequestMethod.GET, RequestMethod.POST})
	public void deleteContacto(HttpServletResponse response, 
			@RequestParam String type, 
			@RequestParam Long asgContactoId, 
			@RequestParam Long identifier) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getAsgContactoService().deleteAsgContacto(asgContactoId);
			result.put("contactos", DBContext.getAsgContactoDao().findAllByType(identifier, type));
			result.put(SUCCESS, true);
			result.put(MESSAGE, UPDATED_DATA);
		} catch (WebException e) {
			logger.error("Exception: ", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		} catch (Exception e) {
			logger.error("Exception: ", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}
}
